<?php
# !!!!!!!!!! DON'T MAKE ANY CHANGE IF YOU DON'T KNOW WHAT YOU'RE DOING !!!!!!!!!!
// --------------------------------------------------------------------------------------------------------------
// File Name              :	users.php
// Last Modification Time	:	2008-02-01 24:00
// Last Modified by       :	turker (turker.biz@gmail.com)
// list of modifications  :
//
//	- 2008-02-01 turker
//		* 24:00 file created.
//
// --------------------------------------------------------------------------------------------------------------
if (eregi("users.php",$_SERVER['PHP_SELF'])) {
	header ("Location:../index.php");
	exit;
}

if (!isAdmin()) {
  header ("Location:$SITE_URL/");
	exit;
}
?>
  <link rel="stylesheet" href="template/src/css/shadowbox.css" type="text/css" media="screen" />
  <script type="text/javascript" src="template/src/js/adapter/shadowbox-jquery.js"></script>
  <script type="text/javascript" src="template/src/js/shadowbox.js"></script>
  <script type="text/javascript">
  $(document).ready(function(){
    var SHBoxOptions = {
      displayNav: false,
      handleUnsupported: 'remove',
      keysClose: ['c', 27] // c or esc
    };
    Shadowbox.init(SHBoxOptions);
  });</script>
<!--div:icerik -->
  <div id="icerik">
    <p>
      <h1>Kullanıcı İşlemleri</h1><br />
<?php
if (empty($_POST) && @empty($_GET['id'])) {
  echo <<<html
    <table cellpadding="2" cellspacing="0" width="99%">
      <tr>
        <th>&nbsp;</th>
        <th>İsim</th>
        <th>E-Mail</th>
        <th align="center">Level</th>
        <th align="center">Tarih</th>
      </tr>
html;
  //id, email, password, name, lang, date, level, bdate, gender, phone, address, city
  $userList=getUsers();
  for ($i=0;$i<count($userList);$i++) {
    $user=$userList[$i];
    $id=$user['id'];
    $email=$prep->html($user['email']);
    $name=$prep->html($user['name']);
    $level=$user['level'];
    $gender=$user['gender'];
    $date=$user['date'];
    /*
  	0 : yeni kayıt
  	1 : email doğrulamış
  	2 : yönetici onaylamış
  	3 : rezerve
  	4 : rezerve
  	5 : yönetici
  	*/
    $date=strftime('%d/%m/%Y %A',$date);
    $date=iconv("ISO-8859-9", "UTF-8",$date);
    if ($level==0) $level='Yeni Kayıt';
    elseif ($level==1) $level='Email Adresi Doğrulanmış';
    elseif ($level==2) $level='Yönetici Onaylamış';
    elseif ($level==3) $level='Rezerve';
    elseif ($level==4) $level='Rezerve';
    elseif ($level==5) $level='Yönetici';
    else $level='Tanımsız';

    echo <<<html
    <tr id="id_$id">
      <td align="center"><input type="checkbox" name="user" value="$id" /></td>
      <td align="left">
        <img src="template/images/$gender.gif" />
        <a href="index.php?page=users&id=$id">$name</a>
      </td>
      <td>$email</td>
      <td align="center">$level</td>
      <td align="right">$date</td>
    </tr>
html;
  }
  echo <<<html
    <tr>
      <td align="center" colspan="5"><hr width="100%" /></td>
    </tr>
    <tr>
      <td colspan="5">&nbsp;&nbsp;&nbsp;&nbsp;
        Seçili olanları <input type="button" id="delall" value=" Sil " onclick="delUsers();" /> veya
        hepsinin seviyesini <select name="level" id="level">
          <option value="0">Yeni Kayıt</option>
          <option value="1">Email Adresi Doğrulanmış</option>
          <option value="2">Yönetici Onaylamış</option>
          <option value="5">Yönetici</option>
        </select> &nbsp;<input type="button" id="setall" value="  olarak ayarla " onclick="changeLevel();" />
      </td>
    </tr>
  </table>
html;
}
elseif (@!empty($_GET['id'])&&empty($_POST)) {
  $id=@(int)$_GET['id'];
  $user=getUser($id);
  if (is_array($user)){
    //id, email, password, name, lang, date, level, bdate, gender, phone, address, city
    foreach ($user as $key=>$val) $user[$key]=$prep->html($val);
    extract($user,EXTR_OVERWRITE);
?>
    <span class="uyari">Yapılan değişiklikler kullanıcıya otomatik olarak iletilmeyecektir.</span>
    <form method="post" action="index.php?page=users" id="regForm" class="box">
    <input type="hidden" name="uid" value="<?php echo $id; ?>" />
    <table cellpadding="2" cellspacing="1" width="100%">
      <tr>
        <td align="right">*Ad Soyad: </td>
        <td><input type="text" value="<?php echo $prep->html($name); ?>" maxlength="55" name="name" id="name" accesskey="req" /></td>
      </tr>
      <tr>
        <td align="right">*E-Mail: </td>
        <td><input type="text" value="<?php echo $email; ?>" maxlength="55" name="email" id="email" accesskey="req" /></td>
      </tr>
      <tr>
        <td align="right">*Telefon: </td>
        <td><input type="text" value="<?php echo $phone; ?>" maxlength="11" name="phone" id="phone" accesskey="req" /></td>
      </tr>
      <tr>
        <td align="right">Adres: </td>
        <td><textarea name="address" id="address"><?php echo $prep->html($address); ?></textarea></td>
      </tr>
      <tr>
        <td align="right">Şehir: </td>
        <td><?php echo listCities($city); ?></td>
      </tr>
      <tr>
        <td align="right">Doğum tarihi: </td>
        <td><?php echo date("d/m/Y",$bdate); ?> &nbsp;&nbsp;&nbsp;&nbsp;Cinsiyet: <img src="template/images/<?php echo $gender; ?>.gif" title="cinsiyet" /></td>
      </tr>
      <tr>
        <td align="right">Vergi Dairesi: </td>
        <td><input type="text" value="<?php echo $prep->html($tax_office); ?>" maxlength="55" name="tax_office" id="tax_office" /></td>
      </tr>
      <tr>
        <td align="right">Vergi Numarası: </td>
        <td><input type="text" value="<?php echo $tax_id; ?>" maxlength="20" name="tax_id" id="tax_id" /></td>
      </tr>
      <tr>
        <td align="right">İndirim Türü: </td>
        <td>
          <input type="radio" name="discount_type" value="ratio" <?php if ($discount_type=='ratio') echo ' checked'; ?> class="radio" /> Oran &nbsp;
          <input type="radio" name="discount_type" value="rebate" <?php if ($discount_type=='rebate') echo ' checked'; ?> class="radio" /> Miktar
        </td>
      </tr>
       <tr>
        <td align="right">İskonto: </td>
        <td><input type="text" value="<?php echo $discount; ?>" maxlength="10" name="discount" id="discount" /></td>
      </tr>
      <tr>
        <td align="right">Seviye: </td>
        <td>
          <select name="level" id="level">
          <option value="0"<?php if ($level=="0") echo " selected"; ?>>Yeni Kayıt</option>
          <option value="1"<?php if ($level=="1") echo " selected"; ?>>Email Adresi Doğrulanmış</option>
          <option value="2"<?php if ($level=="2") echo " selected"; ?>>Yönetici Onaylamış</option>
          <option value="5"<?php if ($level=="5") echo " selected"; ?>>Yönetici</option>
        </select>
        </td>
      </tr>
      <tr>
        <td align="right">Dil: </td>
        <td><?php echo listLangs($lang); ?></td>
      </tr>
      <tr>
        <td align="right">&nbsp;</td>
        <td>
          <input type="submit" value=" Üye Bilgilerini Kaydet " id="reg" class="input-submit" />
        </td>
      </table>
    </form>
    <hr />
    <h2>Yapılan Alışverişler</h2>
    <div id="uyesiparisleri">
    <?php
      $user_id=$user['id'];
      //id, basket_ids, buy, buy_date, send, send_date, address, order_note, admin_note, user_id, total
      $q=$mysql->query("select id,send_date,admin_note,total from $ORDERS_TABLE where buy='1' and send='1' and user_id='$user_id'");
      if ($mysql->numRows($q)>0) {
        echo '<br /><table cellpadding="2" cellspacing="0" width="100%">';
        echo '<tr><th align="center">Tarih</th><th align="center">Site Notu</th><th align="center">Tutar</th></tr>';
        while ($read=$mysql->fetch($q)) {
        	$id=$read['id'];
        	$send_date=$read['send_date'];
        	$total=$read['total'];
        	$admin_note=$prep->html($read['admin_note']);
        	$send_date=strftime("%d %b %y %a %H:%M",$send_date);
        	$send_date=iconv("ISO-8859-9", "UTF-8",$send_date);

        	$send_link="index.php?page=orders&id=$id";
        	echo '<tr><td align="center"><a rel="shadowbox;width=760;height=555" title="Sipariş Detayı" href="'.$send_link.'">'.$send_date.'</a></td>
        	             <td>'.$admin_note.'</td><td align="right">'.$total.' YTL</td></tr>';
        }
        echo '</table><br />';
      }
      else echo '<br />Henüz alışveriş yapılmamış.<br />';
    ?>
    </div>
<?php
  }
  else echo 'geçersiz kullanım';
}
elseif (!empty($_POST)) {
  # prepare $_POST
  foreach ($_POST as $key=>$val) $_POST[$key]=trim($val);
  extract($_POST,EXTR_OVERWRITE);

  # check $_POST
  $valid=new Validation();
  $valid->inRange($name,55,6,'Ad en az 6 en fazla 55 karakter olmalı');
  $valid->inRange($email,55,6,'E-Mail en az 6 en fazla 55 karakter olmalı');
  $valid->inRange($phone,11,7,'Telefon en az 7 en fazla 11 karakter olmalı');
  $valid->checkEmail($email,'Geçersiz email');
  if (!empty($discount)) $valid->isNumber($discount,'Geçerli iskonto miktarı girin');
  if (!empty($tax_id)) $valid->isNumber($tax_id,'Geçerli vergi numarası girin');
  if ($valid->isError()) { # error!
    $valid->listErrors();
  }
  else { #no error
    $email=$mysql->escape($email);
    $q=$mysql->query("select id from $USERS_TABLE where email='$email' and id!='$uid'");
    if ($mysql->numRows($q)>0) echo 'E-Mail kullanımda, farklı email adresi seçin<br />';
    else {
      foreach ($_POST as $key=>$val) $_POST[$key]=$mysql->escape($val);
      extract($_POST,EXTR_OVERWRITE);
      $discount=str_replace(',','.',$discount);
      // id, email, password, name, lang, date, level, bdate, gender, phone, address, city, discount, discount_type, tax_id, tax_office
      $q=$mysql->query("update $USERS_TABLE set phone='$phone',address='$address',discount='$discount',discount_type='$discount_type',tax_id='$tax_id',"
                      ."tax_office='$tax_office',name='$name',email='$email',city='$city',lang='$lang',level='$level' where id='$uid'");
      if ($q) echo '<br />Kullanıcı bilgileri güncellendi.<br />';
      else echo '<br />Hata<br />';
    } // else
  }
  echo goBack();
}
?>
<a rel="shadowbox;width=760;height=555" title="fix" href=""></a>
    </p>
  </div>
<!--//div:icerik -->